package database;

import log4j.UtilLog4j;
import number.UtilNumber;
import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

/**
 * Description:
 * Created by 4790K_NSL on 2017/5/20.
 */
public class UtilPreparedStatment {
    static private Logger logger=UtilLog4j.getLogger(UtilPreparedStatment.class);

    /** 目前支持的类型为： int;bigint,float,double,varchar
     * @param ps
     * @param index
     * @param intType
     * @param value
     * @throws SQLException
     */
   static public void setPsParameter(PreparedStatement ps, int index, int intType, String value) throws SQLException {
       logger.debug("ps index = "+index);
       logger.debug("insert intType = "+intType);
       logger.debug("insert value = "+value);
        if(value==null){
           ps.setNull(index,intType);
        }
        value=value.replaceAll("\"","");
       //logger.debug(value);
       value=value.trim();
        switch (intType){
            case Types.BIGINT:
                if(value.equalsIgnoreCase("")){
                    ps.setNull(index,intType);
                }else{
                    Long l=Long.parseLong(value);
                    ps.setLong(index,l);
                }
                break;
            case Types.FLOAT:
                if(value.equals("")){
                    ps.setNull(index,intType);
                }else {
                    Float f= UtilNumber.parseFloat(value);
                    ps.setFloat(index,f);
                }
                break;
            case Types.DOUBLE:
                if(value.equals("")){
                    ps.setNull(index,intType);
                }else {
                    Double d= UtilNumber.parseDouble(value);
                    ps.setDouble(index,d);
                }
                break;
            case Types.VARCHAR:
                ps.setString(index,value);
                break;
            default:
                throw new RuntimeException("Unexpected database type.");
        }
    }
}
